//
//  HNVideoDetailController.m
//  XDEducation
//
//  Created by 星道 on 17/5/31.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNVideoDetailController.h"
#import "HNVideoDetailHeaderCell.h"
#import "WMPlayer.h"
#import "AppDelegate.h"

@interface HNVideoDetailController ()<WMPlayerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    BOOL _HengPing;
}
@property (nonatomic, strong) UITableView *TableView;
@property (nonatomic, strong) WMPlayer *Player;
@property (nonatomic, strong) AppDelegate *Appdelegate;
@end

@implementation HNVideoDetailController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.Player];
    [self.view addSubview:self.TableView];
    [self.Player play];
}



#pragma mark - 视频控制
/** 全屏 */
-(void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn
{
    _HengPing = !_HengPing;
    if (_HengPing)
    {
        //切换成横屏
        [self HengPing];
    }
    else
    {
        [self ShuPing];
    }
    
}

//横屏
- (void)HengPing
{
    //强制旋转横屏
    self.Appdelegate.allowRotation = 1;
    //强制翻转屏幕，Home键在右边。
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
    self.Player.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    
    CGFloat x = 0;
    CGFloat y = self.Player.frame.size.height;
    CGFloat w = ScreenW;
    CGFloat h = ScreenH - self.Player.frame.size.height;
    _TableView.frame = CGRectMake(x, y, w, h);
}
//竖屏
- (void)ShuPing
{
    //强制旋转竖屏
    self.Appdelegate.allowRotation = 0;
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    //刷新
    [UIViewController attemptRotationToDeviceOrientation];
    self.Player.frame = CGRectMake(0, 0, ScreenW, 200*(ScreenH/480.0));
    
    CGFloat x = 0;
    CGFloat y = self.Player.frame.size.height;
    CGFloat w = ScreenW;
    CGFloat h = ScreenH - self.Player.frame.size.height;
    _TableView.frame = CGRectMake(x, y, w, h);
}

/** 停止播放并退出界面 */
-(void)wmplayer:(WMPlayer *)wmplayer clickedCloseButton:(UIButton *)closeBtn
{
    [self ShuPing];
    [self dismissViewControllerAnimated:YES completion:nil];
}





#pragma mark - 表单代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        HNVideoDetailHeaderCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell" forIndexPath:indexPath];
        Cell.Model = self.ListModel;
        return Cell;
    }
    else
    {
        return nil;
    }
}











#pragma mark -Setter
- (WMPlayer *)Player
{
    if (!_Player)
    {
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = ScreenW;
        CGFloat h = 200*(ScreenH/480.0);
        _Player = [[WMPlayer alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _Player.delegate = self;
        [_Player setURLString:self.ListModel.absoluteUrl];
    }
    return _Player;
}

- (AppDelegate *)Appdelegate
{
    if (!_Appdelegate)
    {
        _Appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    }
    return _Appdelegate;
}

- (UITableView *)TableView
{
    if (!_TableView)
    {
        CGFloat x = 0;
        CGFloat y = self.Player.frame.size.height;
        CGFloat w = ScreenW;
        CGFloat h = ScreenH - self.Player.frame.size.height;
        _TableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:(UITableViewStylePlain)];
        [_TableView registerNib:[UINib nibWithNibName:[HNVideoDetailHeaderCell description] bundle:nil] forCellReuseIdentifier:@"HeaderCell"];
        _TableView.tableHeaderView = [UIView new];
        _TableView.tableFooterView = [UIView new];
        _TableView.estimatedRowHeight = 300;
        _TableView.rowHeight = UITableViewAutomaticDimension;
        _TableView.delegate = self;
        _TableView.dataSource = self;

    }
    return _TableView;
}



#
@end
